package com.yc.book.ui;

import org.eclipse.swt.widgets.Composite;
import swing2swt.layout.BorderLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.book.biz.BizException;
import com.yc.book.biz.LeadBiz;
import com.yc.book.biz.ReaderBiz;
import com.yc.book.util.Utils;

import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class BookCmp extends Composite {

	Label label;
	Label label_1;
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public BookCmp(Composite parent, int style) {
		super(parent, style);
		setLayout(new BorderLayout(0, 0));
		
		label = new Label(this, SWT.NONE);
		label.setImage(SWTResourceManager.getImage("D:\\_文件柜\\mp4\\西游记.png"));
		label.setLayoutData(BorderLayout.CENTER);
		
		label_1 = new Label(this, SWT.CENTER);
		label_1.setLayoutData(BorderLayout.SOUTH);
		label_1.setText("西游记");
		
		// 鼠标点击松开之后的事件
		label.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				boolean ret = Utils.confirm(getShell(), "请问是否要借阅该图书?");
				if(ret) {
					Object rid = ReaderBiz.getLoeginedUser().get("ID");
					try {
						biz.lead(rid, bid);
						Utils.showMessage(getShell(), "图书借出成功!");
					} catch (BizException e1) {
						e1.printStackTrace();
						Utils.showMessage(getShell(), e1.getMessage());
					}
				}
			}
		});
	}
	
	private LeadBiz biz = new LeadBiz();
	private Object bid;// 当前图书的id
	
	public BookCmp(Composite parent, int style, String image, String name, 
			// 当前图书的id
			Object bid) { 
		this(parent,style);
		label.setImage(SWTResourceManager.getImage(image));
		label_1.setText(name);
		this.bid = bid;
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
